package com.huika.dealontop.control.common;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.huika.dealontop.R;
import com.huika.dealontop.config.Constants;
import com.huika.dealontop.config.UrlConstants;
import com.huika.dealontop.utils.PreferHelper;

/**
 * @description：军事禁区，一般用户请绕行，否则后果自负， 嘿嘿……
 * @author zhoukl
 * @date 2014年10月29日 上午11:30:45
 */
public class SetEnvironmentActivity extends Activity implements OnClickListener {

	private RadioGroup rgEnvironment;
	private RadioButton mrbRelease, mrbReleaseTest, mrbTest;
	private TextView tvServerDomain;
	private Button btnOk, btnCancel;
	private boolean mIsReleaseEnv;// 正式环境
	private boolean mIsPreReleaseEnv;// 预生产环境

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_set_environment);
		mrbRelease = (RadioButton) findViewById(R.id.rbRelease);
		mrbReleaseTest = (RadioButton) findViewById(R.id.rbReleaseTest);
		mrbTest = (RadioButton) findViewById(R.id.rbTest);
		btnOk = (Button) findViewById(R.id.btnOk);
		btnCancel = (Button) findViewById(R.id.btnCancel);
		btnOk.setOnClickListener(this);
		btnCancel.setOnClickListener(this);

		tvServerDomain = (TextView) findViewById(R.id.tvServerDomain);

		rgEnvironment = (RadioGroup) findViewById(R.id.rgEnvironment);
		rgEnvironment.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.rbRelease:
					tvServerDomain.setText(UrlConstants.RELEASE_SERVER_DOMAIN);
					break;
				case R.id.rbReleaseTest:
					tvServerDomain.setText(UrlConstants.RELEASE_TEST_SERVER_DOMAIN);
					break;
				case R.id.rbTest:
					tvServerDomain.setText(UrlConstants.TEST_SERVER_DOMAIN);
					break;
				}
			}

		});

		// 默认正式环境
		mIsReleaseEnv = PreferHelper.getInstance().getBoolean(Constants.IS_RELEASE_ENVIRONMENT, true);
		mIsPreReleaseEnv = PreferHelper.getInstance().getBoolean(Constants.IS_RELEASE_TEST_ENVIRONMENT, false);
		if (mIsReleaseEnv) {
			mrbRelease.setChecked(true);
		} else if (mIsPreReleaseEnv) {
			mrbReleaseTest.setChecked(true);
		} else {
			mrbTest.setChecked(true);
		}
	}

	protected void switchVersionEnvironment() {
		// TODO: 显示测试版本，发布版本的开关
		if (mrbRelease.isChecked()) {
			PreferHelper.getInstance().setBoolean(Constants.IS_RELEASE_ENVIRONMENT, true);
			PreferHelper.getInstance().setBoolean(Constants.IS_RELEASE_TEST_ENVIRONMENT, false);
			PreferHelper.getInstance().setBoolean(Constants.IS_TEST_ENVIRONMENT, false);
		} else if (mrbReleaseTest.isChecked()) {
			PreferHelper.getInstance().setBoolean(Constants.IS_RELEASE_ENVIRONMENT, false);
			PreferHelper.getInstance().setBoolean(Constants.IS_RELEASE_TEST_ENVIRONMENT, true);
			PreferHelper.getInstance().setBoolean(Constants.IS_TEST_ENVIRONMENT, false);
		} else if (mrbTest.isChecked()) {
			PreferHelper.getInstance().setBoolean(Constants.IS_RELEASE_ENVIRONMENT, false);
			PreferHelper.getInstance().setBoolean(Constants.IS_RELEASE_TEST_ENVIRONMENT, false);
			PreferHelper.getInstance().setBoolean(Constants.IS_TEST_ENVIRONMENT, true);
		}
		/** 发送下线通知给服务器 */
		Toast.makeText(this, "正在切换版本环境...", Toast.LENGTH_SHORT).show();
		finish();
		// 强制清空版本信息， 促使系统重新处理【yfc_res.zip】包
		// getSharedPreferences("init", 0).edit().putInt("version_name_int",
		// 0).commit();

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnOk:
			switchVersionEnvironment();
			break;
		case R.id.btnCancel:
			finish();
		}

	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		System.exit(0);
	}
}
